package com.zdsoft.blp.zongbuju.shouye.taoshihui.activity;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;

import com.zdsoft.blp.R;
import com.zdsoft.blp.gongyong.API.API;
import com.zdsoft.blp.gongyong.fanhui.FanHuiView;
import com.zdsoft.blp.gongyong.net.BaseActivity;
import com.zdsoft.blp.gongyong.net.Net;
import com.zdsoft.blp.gongyong.net.NetCallback;
import com.zdsoft.blp.gongyong.xlistview.XListView;
import com.zdsoft.blp.zongbuju.shouye.taoshihui.adapter.ListViewAdapter;
import com.zdsoft.blp.zongbuju.shouye.taoshihui.entity.Goods;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by pc on 2016/9/1.
 */
public class FenLeiActivity extends BaseActivity implements NetCallback,XListView.IXListViewListener{
    private FanHuiView fh;
    private XListView xlv;
    private int type;
    private static final int FENLEI = 1;
    private static final int DIANPU = 2;
    private static final int REMAI = 3;
    private static final int SOUSUO = 7;
    private static final int HUOQULEIBIEGOODS = 4;
    private static final int HUOQUREMAIGOODS = 5;
    private static final int HUOQUDIANPUGOODS = 6;
    private static final int HUOQUSOUSUOGOODS = 8;
    private SharedPreferences sp;
    private Integer pageNo = 0;
    private List<Goods> goodsList  = new ArrayList<>();
    private ListViewAdapter adapter = null;
    @Override
    protected void init() {
        setContentView(R.layout.tsh_fenlei);
        sp = getSharedPreferences("info", Context.MODE_PRIVATE);
        fh = find(R.id.fh);
        xlv = find(R.id.xlv);
        type = getIntent().getIntExtra("type", 0);
        if (type == FENLEI) {
            fh.getTv_title().setText(getIntent().getStringExtra("name").toString());
            fenLeiData(getIntent().getStringExtra("code"));
        } else if (type == DIANPU) {
            dianPuData(getIntent().getStringExtra("merchantName").toString());
            fh.getTv_title().setText(getIntent().getStringExtra("merchantName").toString());
        }else if(type ==REMAI){
            fh.getTv_title().setText(getIntent().getStringExtra("name").toString());
            reMaiData();
        }else if(type ==SOUSUO){
            fh.getTv_title().setText("搜索结果");
            souSuoData(getIntent().getStringExtra("name").toString());
        }

        xlv.setPullLoadEnable(true);
        xlv.setPullRefreshEnable(true);
        xlv.setXListViewListener(this);

        xlv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent = new Intent(getApplicationContext(),ShangpxqActivity.class);
                intent.putExtra("goodsId",goodsList.get(position-1).getId());
                startActivity(intent);
            }
        });
    }

    private void dianPuData(String merchantName) {
        String xiaoQuCode = sp.getString("communityCode", "00001");
        Net net = new Net();
        net.url = API.GOODS_URL;
        net.nameSpace = API.GOODS_NAMESPACE;
        net.methodName = API.SPGOODSLIST;
        Map<String, Object> map = new HashMap<>();
        map.put("pageNo", pageNo);
        map.put("pageSize", 10);
        map.put("goodsCategoryCode", "");
        map.put("type", "P");
        map.put("curTime", new Date().getTime() + "");
        map.put("communityCode", xiaoQuCode);
        map.put("condition", merchantName);
        net.parms = map;
        net.code = HUOQUDIANPUGOODS;
        getServer(net, FenLeiActivity.this);
    }
    private void souSuoData(String name) {
        String xiaoQuCode = sp.getString("communityCode", "00001");
        Net net = new Net();
        net.url = API.GOODS_URL;
        net.nameSpace = API.GOODS_NAMESPACE;
        net.methodName = API.SPGOODSLIST;
        Map<String, Object> map = new HashMap<>();
        map.put("pageNo", pageNo);
        map.put("pageSize", 10);
        map.put("goodsCategoryCode", "");
        map.put("type", "P");
        map.put("curTime", new Date().getTime() + "");
        map.put("communityCode", xiaoQuCode);
        map.put("condition", name);
        net.parms = map;
        net.code = HUOQUSOUSUOGOODS;
        getServer(net, FenLeiActivity.this);
    }

    private void fenLeiData(String classcode) {
        String xiaoQuCode = sp.getString("communityCode", "00001");
        Net net = new Net();
        net.url = API.GOODS_URL;
        net.nameSpace = API.GOODS_NAMESPACE;
        net.methodName = API.SPGOODSLIST;
        Map<String, Object> map = new HashMap<>();
        map.put("pageNo", pageNo);
        map.put("pageSize", 10);
        map.put("goodsCategoryCode", classcode);
        map.put("type", "P");
        map.put("curTime", new Date().getTime() + "");
        map.put("communityCode", xiaoQuCode);
        map.put("condition", "");
        net.parms = map;
        net.code = HUOQULEIBIEGOODS;
        getServer(net, FenLeiActivity.this);
    }
    private void reMaiData() {
        String code = sp.getString("communityCode", "00001");
        Net net = new Net();
        net.url = API.GOODS_URL;
        net.nameSpace = API.GOODS_NAMESPACE;
        net.methodName = API.SORTGOODSLIST;
        Map<String, Object> map = new HashMap<>();
        map.put("pageNo", pageNo);
        map.put("pageSize", 10);
        map.put("communityCode", code);
        map.put("type", "P");
        map.put("curTime", new Date().getTime() + "");
        net.parms = map;
        net.code = HUOQUREMAIGOODS;
        getServer(net, FenLeiActivity.this);
    }


    @Override
    public void onResponse(boolean flag, int code, String res) {
        if (flag && code == HUOQULEIBIEGOODS||code ==HUOQUREMAIGOODS||code==HUOQUDIANPUGOODS||code==HUOQUSOUSUOGOODS) {
            try {
                JSONObject object = new JSONObject(res);
                JSONArray array = object.getJSONArray("rows");
                for (int i = 0; i < array.length(); i++) {
                    JSONObject jsonObject = array.getJSONObject(i);
                    goodsList.add(new Goods(jsonObject.getString("total"), jsonObject.getString("summary"), jsonObject.getString("oldPrice"),
                            jsonObject.getString("canBuy"), jsonObject.getString("limitNum"), jsonObject.getString("discountPrice"), jsonObject.getString("goodsCode"),
                            jsonObject.getString("goodsNum"), jsonObject.getString("picPath"), jsonObject.getString("id"), jsonObject.getString("peopleBuy"),
                            jsonObject.getString("idsort"), jsonObject.getString("tgoodscategory"), jsonObject.getString("discountStartDate"), jsonObject.getString("goodsName"),
                            jsonObject.getString("discountEndDate"), jsonObject.getString("tgoodscategoryid"), jsonObject.getString("merchantname")));
                }
                if (pageNo == 0) {
                    adapter = new ListViewAdapter(goodsList, getApplicationContext());
                    xlv.setAdapter(adapter);
                    pageNo++;
                    xlv.stopRefresh();
                } else {
                    adapter.setLists(goodsList);
                    adapter.notifyDataSetChanged();
                    xlv.stopLoadMore();
                }
                pageNo++;
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
    }
    @Override
    public void onRefresh() {
        goodsList  = new ArrayList<>();
        pageNo=0;
        if (type == FENLEI) {
            fenLeiData(getIntent().getStringExtra("code"));
        } else if (type == DIANPU) {
            dianPuData(getIntent().getStringExtra("merchantName").toString());
        }else if (type == REMAI) {
            reMaiData();
        }else if (type == SOUSUO) {
            souSuoData(getIntent().getStringExtra("name").toString());
        }
    }

    @Override
    public void onLoadMore() {
        if (type == FENLEI) {
            fenLeiData(getIntent().getStringExtra("code"));
        } else if (type == DIANPU) {
            dianPuData(getIntent().getStringExtra("merchantName").toString());
        }else if (type == REMAI) {
            reMaiData();
        }else if (type == SOUSUO) {
            souSuoData(getIntent().getStringExtra("name").toString());
        }
    }
}
